package com.lantosec.hmrp.modules.monitor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.util.MonitorUtils;
import com.lantosec.hmrp.modules.monitor.entity.WarningDataEntity;
import com.lantosec.hmrp.modules.monitor.entity.WarningTotalEntity;
import com.lantosec.hmrp.modules.monitor.mapper.MonitorWarningMapper;
import com.lantosec.hmrp.modules.monitor.service.IMonitorWarningService;
import com.lantosec.hmrp.modules.warning.service.IWarningDataService;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service()
@Primary
@Log4j2
@SuppressWarnings("all")
public class MonitorWarningServiceImpl extends ServiceImpl<MonitorWarningMapper, WarningTotalEntity> implements IMonitorWarningService {

    @Autowired
    private IWarningDataService warningDataService;

    @Override
    public WarningTotalEntity getWarningTotal(String month, String year) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            startTime = month + MonitorUtils.firstDayOfMonth();
            endTime = month + MonitorUtils.lastDayOfMonth();
        } else if (StringUtils.isNotBlank(year)) {
            startTime = MonitorUtils.firstDayOfYear(year);
            endTime = MonitorUtils.lastDayOfYear(year);
        } else {
            startTime = MonitorUtils.getLastMonthFirstDay();
            endTime = MonitorUtils.getLastMonthLastDay();
        }
        WarningTotalEntity param = baseMapper.getWarningTotalByYear(startTime, endTime);
        return param;
    }

    /**
     * @date: 2020-06-24 10:14
     * @instructions: 按预警处置率倒序排
     * @param:
     * @return:
     */
    @Override
    public List<WarningTotalEntity> getWarningDisposalRate(String month, String year) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            startTime = month + MonitorUtils.firstDayOfMonth();
            endTime = month + MonitorUtils.lastDayOfMonth();
        } else if (StringUtils.isNotBlank(year)) {
            startTime = MonitorUtils.firstDayOfYear(year);
            endTime = MonitorUtils.lastDayOfYear(year);
        } else {
            startTime = MonitorUtils.getLastMonthFirstDay();
            endTime = MonitorUtils.getLastMonthLastDay();
        }
        List<WarningTotalEntity> param = baseMapper.getWarningDisposalRate(startTime, endTime);
        return param;
    }


    /**
     * @date: 2020-06-24 11:08
     * @instructions:
     * @param: type:1全部，2已出置，3未处置，4违规
     * @return:
     */
    @Override
    public List<WarningTotalEntity> getWarningDetailsList(String month, String year, Integer type,String deptId) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            startTime = month + MonitorUtils.firstDayOfMonth();
            endTime = month + MonitorUtils.lastDayOfMonth();
        } else if (StringUtils.isNotBlank(year)) {
            startTime = MonitorUtils.firstDayOfYear(year);
            endTime = MonitorUtils.lastDayOfYear(year);
        } else {
            startTime = MonitorUtils.getLastMonthFirstDay();
            endTime = MonitorUtils.getLastMonthLastDay();
        }
        List<WarningTotalEntity> param = baseMapper.getWarningDetailsList(startTime, endTime, type,deptId);
        return param;
    }


    /**
     * @date: 2020-06-24 15:19
     * @instructions: type:1全部，2已出置，3未处置，4违规
     * @param:
     * @return:
     */
    @Override
    public Page<WarningDataEntity> getWarningData(PageVO<WarningDataEntity> page, String month,
                                                  String year, Integer type, String ruleId, String deptId) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            startTime = month + MonitorUtils.firstDayOfMonth();
            endTime = month + MonitorUtils.lastDayOfMonth();
        } else if (StringUtils.isNotBlank(year)) {
            startTime = MonitorUtils.firstDayOfYear(year);
            endTime = MonitorUtils.lastDayOfYear(year);
        } else {
            startTime = MonitorUtils.getLastMonthFirstDay();
            endTime = MonitorUtils.getLastMonthLastDay();
        }
        PageVO<WarningDataEntity> param = baseMapper.getWarningData(page, startTime, endTime, type, ruleId, deptId);
        return page.setRecords(param.getRecords());
    }

    @Override
    public List<WarningTotalEntity> getChargeDeptRate(String month, String year) {
        String startTime = null;
        String endTime = null;
        if (StringUtils.isNotBlank(month)) {
            startTime = month + MonitorUtils.firstDayOfMonth();
            endTime = month + MonitorUtils.lastDayOfMonth();
        } else if (StringUtils.isNotBlank(year)) {
            startTime = MonitorUtils.firstDayOfYear(year);
            endTime = MonitorUtils.lastDayOfYear(year);
        } else {
            startTime = MonitorUtils.getLastMonthFirstDay();
            endTime = MonitorUtils.getLastMonthLastDay();
        }
        List<WarningTotalEntity> param = baseMapper.getChargeDeptRate(startTime, endTime);
        return param;
    }


    /**
     * @date: 2020-06-29 17:08
     * @instructions: 预警工作台已办汇总
     * @param:
     * @return:
     */
    @Override
    public Page<WarningDataEntity> getWarningWorkbench(PageVO<WarningDataEntity> page, String userId) {
        PageVO<WarningDataEntity> param = baseMapper.getWarningWorkbench(page, userId);
        return param;
    }


    /**
     * @date: 2020-06-29 17:08
     * @instructions: 预警工作台已办详情
     * @param:
     * @return:
     */
    @Override
    public Page<WarningDataEntity> getWarningHaveDoneDetails(PageVO<WarningDataEntity> page, String userId, String ruleId) {
        PageVO<WarningDataEntity> param = baseMapper.getWarningHaveDoneDetails(page, userId, ruleId);
        return param;
    }


    /**
     * @date: 2020-06-29 17:09
     * @instructions: 预警工作台待办汇总
     * @param:
     * @return:
     */
    @Override
    public List<WarningDataEntity> getWarningBacklog(String userId) {
        List<WarningDataEntity> result = new ArrayList<>();
        //当前用户是否是预警处置人或审核人
        List<WarningDataEntity> userList = baseMapper.getUserList();
        for (WarningDataEntity entity : userList) {
            if (userId.equals(entity.getUserId())) {
                WarningDataEntity param = baseMapper.getWarningBacklog(entity.getRuleId());
                if(StringUtils.isNotEmpty(param.getRuleId())){
                    result.add(param);
                }
            }
        }
        return result;
    }

    /**
     * @date: 2020-06-29 17:08
     * @instructions: 预警工作台待办详情
     * @param:
     * @return:
     */
    @Override
    public Page<WarningDataEntity> getWarningBacklogDetails(PageVO<WarningDataEntity> page, String ruleId) {
        PageVO<WarningDataEntity> param = baseMapper.getWarningBacklogDetails(page, ruleId);
        return param;
    }
}
